﻿package com.tudou.home.newskin.panel
{
    import com.tudou.consts.*;
    import com.tudou.cookie.*;
    import com.tudou.home.common.events.*;
    import com.tudou.home.newskin.consts.*;
    import com.tudou.home.newskin.containers.*;
    import com.tudou.home.newskin.events.*;
    import com.tudou.home.utils.*;
    import com.tudou.js.*;
    import com.tudou.skin.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class InfoPanel extends Sprite implements ITipInfo, IRate, IOriginalInfo, IPlayer, ITransform, ISkipHeadTail, ITime, ISetting
    {
        private var isReady:Boolean;
        private var rateSettingData:RateSettingData;
        private var tip:InfoSprite;
        private var playTime:Number;
        private var skipSettingData:SkipSettingData;
        private var background:DisplayObject;
        private var totalTime:Number;
        private var hasSkip:Boolean = false;
        private var rateType:int;
        private var closeBtn:SimpleButton;
        private var timer:Timer;
        private var currentPriority:int;
        private var originalRate:Number;
        private var original:Boolean;
        private var originalHeight:int;
        private var maxRate:int;
        private var skipTail:Number;
        private var originalWidth:int;
        private var skipHead:Number;
        private static const FAST:int = 500;
        private static const HAS_SKIP:int = 900;
        private static const V2_ERROR:int = 1200;
        private static const UNGILIVABLE:int = 600;
        private static const SKIP_INFO:int = 1000;
        private static const HAS_ORIGINAL:int = 700;
        public static const CLOSE_BUTTON:int = 10000;
        private static const ORIGINAL_INFO:int = 800;

        public function InfoPanel()
        {
            var _loc_1:* = new TipBackgroundMC();
            _loc_1.mouseEnabled = false;
            _loc_1.useHandCursor = false;
            background = _loc_1;
            addChild(background);
            tip = new InfoSprite();
            tip.x = 8;
            tip.y = 6;
            tip.addEventListener(TextEvent.LINK, textLinkHlr);
            tip.addEventListener(SkipEvent.OPEN, skipOpenHlr);
            tip.addEventListener(SkipEvent.SETTING, skipSettingHlr);
            addChild(tip);
            closeBtn = new CloseTipBTN();
            closeBtn.x = background.width - closeBtn.width - 4;
            closeBtn.y = int((background.height - closeBtn.height) / 2);
            addChild(closeBtn);
            closeBtn.addEventListener(MouseEvent.CLICK, clickHandler);
            this.visible = false;
            return;
        }// end function

        public function setSkipHeadTailValue(param1:Number, param2:Number) : void
        {
            skipHead = param1;
            skipTail = param2;
            return;
        }// end function

        public function setRate(param1:int) : void
        {
            rateType = param1;
            if (!isReady)
            {
                return;
            }
            if (rateType == RateType.ORIGINAL)
            {
                CenterDisptcher.getInst().dispatchEvent(new CenterEvent(NewSkinConsts.ARROW_TIP_EVENT, [NewSkinConsts.ARROW_HIDE]));
            }
            else if (currentPriority == ORIGINAL_INFO)
            {
                clearTipInfo((ORIGINAL_INFO + 1));
            }
            return;
        }// end function

        public function setTotalTime(param1:Number) : void
        {
            totalTime = param1;
            return;
        }// end function

        public function showError(param1:int) : void
        {
            show(V2_ERROR);
            tip.showError(param1);
            updateButton();
            return;
        }// end function

        public function hasSkipHeadTail(param1:Boolean) : void
        {
            hasSkip = param1;
            return;
        }// end function

        public function setRateSetting(param1:RateSettingData) : void
        {
            rateSettingData = param1;
            tip.setRateSetting(param1);
            return;
        }// end function

        private function clearHasOriginalHlr(event:CenterEvent) : void
        {
            var _loc_2:* = CenterDisptcher.getInst();
            _loc_2.removeEventListener(NewSkinConsts.CLEAR_HAS_ORIGINAL, clearHasOriginalHlr);
            dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.CLICK_RATE_SELECTOR_WITH_ARROW));
            clearTipInfo((HAS_ORIGINAL + 1));
            return;
        }// end function

        private function updateButton() : void
        {
            closeBtn.x = tip.x + tip.width + 6;
            return;
        }// end function

        private function skipSettingHlr(event:SkipEvent) : void
        {
            dispatchEvent(new SkinEvent(SkinEvent.EXIT_FULLSCREEN));
            var _loc_2:Object = {rate:rateSettingData, skip:skipSettingData};
            JSExchanger.getInst().notifyJS("settingPanel", ["skip", _loc_2]);
            clearTipInfo(CLOSE_BUTTON);
            dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.SETTING));
            return;
        }// end function

        private function showHasSkipTip() : void
        {
            show(HAS_SKIP);
            tip.showHasSkipTip();
            updateButton();
            autoClose(NewSkinConsts.SKIP_TIP_TIME);
            return;
        }// end function

        private function autoClose(param1:Number) : void
        {
            if (timer)
            {
                timer.stop();
                timer.removeEventListener(TimerEvent.TIMER_COMPLETE, autoCloseHlr);
                timer = null;
            }
            timer = new Timer(param1, 1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, autoCloseHlr);
            timer.start();
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            if (currentPriority == HAS_ORIGINAL)
            {
                dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.NO_MORE_TIP_X));
            }
            clearTipInfo(CLOSE_BUTTON);
            return;
        }// end function

        public function ready() : void
        {
            isReady = true;
            clearTipInfo((FAST + 1));
            setTimeout(_ready, 250);
            return;
        }// end function

        private function _ready() : void
        {
            var _loc_1:Boolean = false;
            if (hasSkip && playTime < totalTime * skipHead)
            {
                if (!PlayerSO.getInst().getUsed(CookieConsts.NO_FIRST_SKIP_TIP))
                {
                    _loc_1 = true;
                    showHasSkipTip();
                }
            }
            if (original && rateType != RateType.ORIGINAL)
            {
                if (!PlayerSO.getInst().getUsed(CookieConsts.NO_ORIGINAL_TIP))
                {
                    if (!_loc_1)
                    {
                        showHasOriginalTip();
                    }
                    else
                    {
                        setTimeout(showHasOriginalTip, NewSkinConsts.SKIP_TIP_TIME + 100);
                    }
                }
            }
            setRate(rateType);
            return;
        }// end function

        private function autoCloseHlr(event:TimerEvent) : void
        {
            clearTipInfo(CLOSE_BUTTON);
            return;
        }// end function

        private function textLinkHlr(event:TextEvent) : void
        {
            if (event.text == RateType.P480.toString())
            {
                dispatchEvent(new SkinEvent(SkinEvent.SET_RATE, RateType.P480));
                clearTipInfo(CLOSE_BUTTON);
            }
            else if (event.text == "tudouService")
            {
                openURL("http://www.tudou.com/service/?type=play#form");
            }
            else if (event.text == "noTip")
            {
                PlayerSO.getInst().setUsed(CookieConsts.NO_ORIGINAL_TIP, true);
                clearTipInfo(CLOSE_BUTTON);
                dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.NO_MORE_TIP));
            }
            else if (event.text == "noSkipTip")
            {
                PlayerSO.getInst().setUsed(CookieConsts.NO_FIRST_SKIP_TIP, true);
                clearTipInfo(CLOSE_BUTTON);
            }
            return;
        }// end function

        public function setSkipSeting(param1:SkipSettingData) : void
        {
            skipSettingData = param1;
            tip.setSkipSeting(param1);
            return;
        }// end function

        public function showSettingSkipTip() : void
        {
            clearTipInfo((HAS_SKIP + 1));
            show(SKIP_INFO);
            tip.showSettingSkipTip();
            updateButton();
            autoClose(NewSkinConsts.SKIP_TIP_TIME);
            return;
        }// end function

        public function playing() : void
        {
            clearTipInfo((UNGILIVABLE + 1));
            return;
        }// end function

        public function loading() : void
        {
            return;
        }// end function

        override public function get height() : Number
        {
            return background.height;
        }// end function

        public function setOriginalWidthHeight(param1:int, param2:int) : void
        {
            originalWidth = param1;
            originalHeight = param2;
            return;
        }// end function

        public function showSettingedSkipTip() : void
        {
            return;
        }// end function

        public function completed() : void
        {
            clearTipInfo(CLOSE_BUTTON);
            return;
        }// end function

        public function setMaxRate(param1:int, param2:Boolean) : void
        {
            maxRate = param1;
            this.original = param2;
            return;
        }// end function

        public function paused() : void
        {
            clearTipInfo((UNGILIVABLE + 1));
            return;
        }// end function

        public function setOriginalRate(param1:Number) : void
        {
            originalRate = param1;
            return;
        }// end function

        private function skipOpenHlr(event:SkipEvent) : void
        {
            clearTipInfo(CLOSE_BUTTON);
            var _loc_2:* = new SkipSettingData({skipIntro:1, skipOutro:1});
            dispatchEvent(new SkinEvent(SkinEvent.SAVE_SKIP_SETTING, _loc_2));
            return;
        }// end function

        public function getRate() : int
        {
            return rateType;
        }// end function

        public function showFastTip(param1:Boolean = false) : void
        {
            show(FAST);
            tip.showFastTip(param1);
            updateButton();
            return;
        }// end function

        public function clearTipInfo(param1:int) : void
        {
            if (currentPriority >= param1)
            {
                return;
            }
            currentPriority = -1;
            if (timer)
            {
                timer.stop();
                timer.removeEventListener(TimerEvent.TIMER_COMPLETE, autoCloseHlr);
                timer = null;
            }
            tip.clearTipInfo(param1);
            this.visible = false;
            CenterDisptcher.getInst().dispatchEvent(new CenterEvent(NewSkinConsts.ARROW_TIP_EVENT, [NewSkinConsts.ARROW_HIDE]));
            return;
        }// end function

        public function showTip(param1:int) : void
        {
            tip.showTip(param1);
            if (param1 == 0)
            {
                show(UNGILIVABLE);
            }
            updateButton();
            return;
        }// end function

        public function buffering() : void
        {
            return;
        }// end function

        public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            background.width = param1;
            return;
        }// end function

        public function setPlayTime(param1:Number) : void
        {
            playTime = param1;
            return;
        }// end function

        private function showHasOriginalTip() : void
        {
            show(HAS_ORIGINAL);
            var _loc_1:* = CenterDisptcher.getInst();
            _loc_1.dispatchEvent(new CenterEvent(NewSkinConsts.ARROW_TIP_EVENT, [NewSkinConsts.ARROW_SHOW]));
            _loc_1.addEventListener(NewSkinConsts.CLEAR_HAS_ORIGINAL, clearHasOriginalHlr);
            tip.showHasOriginalTip();
            updateButton();
            autoClose(NewSkinConsts.ORIGINAL_TIP_TIME);
            dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.HAS_ORIGINAL_TIP));
            return;
        }// end function

        protected function show(param1:int) : void
        {
            if (currentPriority > param1)
            {
                return;
            }
            this.visible = true;
            currentPriority = param1;
            return;
        }// end function

    }
}
